def get_user_input(prompt, cast_to_type):
    while True:
        user_input = input(prompt).strip()
        if not user_input:
            print("输入的信息过少！")
            continue
        try:
            return cast_to_type(user_input)
        except ValueError:
            print(f"输入错误，请确保您输入的是{cast_to_type.__name__}类型的数据。")

def calculate_bmr(weight, height, age, gender):
    try:
        # 这里使用标准的Mifflin-St Jeor公式进行BMR计算
        if gender.lower() == 'male':
            return (10 * weight) + (6.25 * height) - (5 * age) + 5
        else:
            return (10 * weight) + (6.25 * height) - (5 * age) - 161
    except Exception as e:
        print("数据异常！")
        return None

def main():
    while True:
        gender = input("请输入性别 (男/女): ").strip().lower()
        if gender not in ['男', '女']:
            print("请输入正确性别！")
            continue

        weight = get_user_input("请输入体重 (kg): ", float)
        height = get_user_input("请输入身高 (cm): ", float)
        age = get_user_input("请输入年龄: ", int)

        if height < 100 or weight < 20:  # 假设正常成人的身高至少100cm，体重至少20kg
            print("数据异常，请输入正确的信息！")
            continue

        bmr = calculate_bmr(weight, height, age, gender)
        if bmr is None:
            # 如果BMR计算异常，已经在calculate_bmr中打印了“数据异常！”
            continue

        print(f"性别: {gender}")
        print(f"体重: {weight} kg")
        print(f"身高: {height} cm")
        print(f"年龄: {age} 岁")
        print(f"基础代谢率 (大卡): {bmr:.2f} kcal/day\n")  # 添加了单位“kcal/day”

        try:
            if input("是否继续计算? (y/n): ").lower() == 'n':
                raise KeyboardInterrupt
        except KeyboardInterrupt:
            print("程序已结束，感谢使用！")
            break
        finally:
            print("请重新输入，或输入'n'以结束程序。")

if __name__ == "__main__":
    main()